using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Object;
using Material.Entity;
using System.Configuration;

namespace Material.App
{
    public partial class FrmHutangDTL : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        public static int idTransHDR = 0;
        public static int idBarang = 0;
        public static int idTransDTL = 0;
        public static string stateInput = string.Empty;
        public static bool isHeader=false;
        //public static int idBarang;


        private  TblTransHutangDTL tblTransHutangDTL;
        private TblTransHutangDTLEntity tblTransHutangDTLEntity;

        //private TblTransPembBarangHDR tblTransPembBarangHDR;
        //private TblTransPembBarangHDREntity tblTransPembBarangHDREntity;
        public FrmHutangDTL()
        {
            InitializeComponent();
            if (stateInput == "Edit")
            {
                tblTransHutangDTL = new TblTransHutangDTL();
                tblTransHutangDTL.IdTransDTL = idTransDTL;
                tblTransHutangDTLEntity = new TblTransHutangDTLEntity(connStr);
                tblTransHutangDTL = tblTransHutangDTLEntity.GetDataByID(tblTransHutangDTL);
                tglBeli.Value = Convert.ToDateTime(tblTransHutangDTL.TglBayar);
                txtJumlah.Text = tblTransHutangDTL.JumlahBayar.ToString();
               

            }
            else
                ClearTextBox();
        }

        private void ClearTextBox()
        {
            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            txtHarga1.Text = "0";
            txtHarga2.Text = "0";
            txtHarga3.Text = "0";
            txtJumlah.Text = "0";
            txtTotal.Text = "0";

            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearTextBox();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int i = 0;

            //if (isHeader)
            //{
            //    tblTransPembBarangHDR = new TblTransPembBarangHDR();
            //    tblTransPembBarangHDREntity = new TblTransPembBarangHDREntity(connStr);
            //    tblTransPembBarangHDR = tblTransPembBarangHDREntity.GetMaxIDTrans();
            //    tblTransPembBarangHDR.IdTrans = tblTransPembBarangHDR.IdTrans;
            //    tblTransPembBarangHDR.IdBarang = idBarang;
            //    tblTransPembBarangHDR.Created = DateTime.Now;
            //    tblTransPembBarangHDR.CreatedBy = "admin";
            //    tblTransPembBarangHDR.Modified = DateTime.Now;
            //    tblTransPembBarangHDR.ModifiedBy = "admin";
            //    idTransHDR = tblTransPembBarangHDR.IdTrans;
            //    FrmPembBarang.idTrans = tblTransPembBarangHDR.IdTrans;

            //    i = tblTransPembBarangHDREntity.Insert(tblTransPembBarangHDR);
            //}
            //if (i != -1)
            //{
                tblTransHutangDTL = new TblTransHutangDTL();
                tblTransHutangDTLEntity = new TblTransHutangDTLEntity(connStr);

                tblTransHutangDTL.IdTransHDR = idTransHDR;
                tblTransHutangDTL.TglBayar = tglBeli.Value;
                tblTransHutangDTL.JumlahBayar = Convert.ToInt32(txtJumlah.Text.Trim());
               

                if (stateInput.Trim() == "Add")
                {
                    tblTransHutangDTL.CreatedDate = DateTime.Now;
                    tblTransHutangDTL.CreatedBy = FrmLogin.UserID;
                    tblTransHutangDTL.ModifiedDate = DateTime.Now;
                    tblTransHutangDTL.ModifiedBy = FrmLogin.UserID;
                    i = tblTransHutangDTLEntity.Insert(tblTransHutangDTL);
                }
                else
                {
                    tblTransHutangDTL.ModifiedDate = DateTime.Now;
                    tblTransHutangDTL.ModifiedBy = FrmLogin.UserID;
                    tblTransHutangDTL.IdTransDTL = idTransDTL;

                    i = tblTransHutangDTLEntity.Update(tblTransHutangDTL);
                }

                if (i != -1)
                {
                    MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                else
                    MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.Close();
            //}
           


           


        }

        private void FrmPembBarangDTL_Load(object sender, EventArgs e)
        {

        }

        private void txtDisc1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga2.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc1.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
                txtDisc2.Focus();
            }

        }

        private void txtDisc2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga3.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc2.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
            }
        }

        private void txtJumlah_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHarga1.Focus();
                //txtHarga2.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc1.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));
            }
        }

        private void txtJumlah_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }
    }
}